/**
 * Copyright (C) 2018-2020
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.order.service.mapper;

import co.yixiang.common.mapper.CoreMapper;
import co.yixiang.modules.order.domain.YxStoreOrderCartInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
* @author hupeng
* @date 2020-05-12
*/
@Repository
public interface StoreOrderCartInfoMapper extends CoreMapper<YxStoreOrderCartInfo> {

    @Select("SELECT b.id AS goods_id,e.uid,e.level,b.price,c.total_num,c.pay_type,c.pay_price,c.combination_id,c.seckill_id,b.is_free,b.operate_amount,b.commission_type_id,b.is_through_train " +
            "FROM yx_store_order_cart_info a " +
            "LEFT JOIN yx_store_product b ON b.id =a.product_id " +
            "LEFT JOIN yx_store_order c ON a.oid = c.id " +
            "LEFT JOIN yx_user e ON c.uid = e.uid " +
            "WHERE a.oid=#{oid}" )
    HashMap<String,Object> getGoodsclassByOrderid(@Param("oid") Long oid);

    @Select("SELECT COUNT(*) FROM  yx_store_order_cart_info a LEFT JOIN yx_store_product b ON b.id =a.product_id  " +
            "WHERE b.price in(299,399) " +
            "AND a.oid IN ( SELECT id FROM yx_store_order WHERE paid =1 AND uid=#{uid}) " )
    String getOrderVIPCountByUserid(@Param("uid") Long uid);

    @Select("SELECT IFNULL(sum(d.cart_num),0) AS COUNT FROM  (SELECT c.cart_num*b.singular as cart_num from yx_store_order_cart_info a LEFT JOIN yx_store_product b ON b.id =a.product_id  " +
            "LEFT JOIN yx_store_cart c ON c.id =a.cart_id "+
            "WHERE b.is_vip =1 " +
            "AND a.oid IN ( SELECT id FROM yx_store_order WHERE paid =1 and id !=#{orderid} AND (uid in(select uid from yx_user where spread_uid =#{uid}))))d " )
    String getOrderCountByUserid(@Param("uid") Long uid,@Param("orderid") Long orderid);

    @Select("SELECT IFNULL(sum(c.total_price),0) AS totalSum " +
            "FROM yx_store_order_cart_info a " +
            "LEFT JOIN yx_store_cart d ON d.id =a.cart_id "+
            "LEFT JOIN yx_store_product b ON b.id =a.product_id " +
            "LEFT JOIN yx_store_order c ON a.oid = c.id " +
            " LEFT JOIN yx_user yu ON yu.uid =c.uid " +
            "WHERE yu.spread_uid=#{uid} and c.is_del =0 and c.paid=1" )
    String getOrderVIPTotalSumByUserid(@Param("uid") Long uid);

    @Select("SELECT IFNULL(sum(d.cart_num),0) AS COUNT " +
            "FROM yx_store_order_cart_info a " +
            "LEFT JOIN yx_store_cart d ON d.id =a.cart_id "+
            "LEFT JOIN yx_store_product b ON b.id =a.product_id " +
            "LEFT JOIN yx_store_order c ON a.oid = c.id " +
            "WHERE c.uid=#{uid} and c.combination_id=#{combinationId} and a.product_id=#{productId} and c.is_del =0 " )
    String getOrderCountByRebate(@Param("uid") Long uid,@Param("combinationId") Long combinationId,@Param("productId") Long productId);

    @Select("SELECT IFNULL(sum(d.cart_num),0) AS COUNT " +
            "FROM yx_store_order_cart_info a " +
            "LEFT JOIN yx_store_cart d ON d.id =a.cart_id "+
            "LEFT JOIN yx_store_product b ON b.id =a.product_id " +
            "LEFT JOIN yx_store_order c ON a.oid = c.id " +
            "WHERE c.uid=#{uid} and a.product_id=#{productId} and DATE_FORMAT(c.create_time,'%Y-%m') =#{month} and c.is_del =0 " )
    String getOrderCountByFree4pay(@Param("uid") Long uid,@Param("productId") Long productId, @Param("month") String month);


    @Select("SELECT IFNULL(sum(d.cart_num),0) AS COUNT " +
            "FROM yx_store_order_cart_info a " +
            "LEFT JOIN yx_store_cart d ON d.id =a.cart_id "+
            "LEFT JOIN yx_store_product b ON b.id =a.product_id " +
            "LEFT JOIN yx_store_order c ON a.oid = c.id " +
            "WHERE c.uid=#{uid} and c.combination_id=#{combinationId} and a.product_id=#{productId} and c.is_del =0 " )
    String getOrderCountByRebate4pay(@Param("uid") Long uid,@Param("combinationId") Long combinationId,@Param("productId") Long productId);

    @Select("SELECT IFNULL(sum(d.cart_num),0) AS COUNT " +
            "FROM yx_store_order_cart_info a " +
            "LEFT JOIN yx_store_cart d ON d.id =a.cart_id "+
            "LEFT JOIN yx_store_product b ON b.id =a.product_id " +
            "LEFT JOIN yx_store_order c ON a.oid = c.id " +
            "WHERE c.uid=#{uid} and c.combination_id=#{combinationId} and a.product_id=#{productId} and c.is_del =0 and c.paid=1" )
    String getOrderCountByRebate4apay(@Param("uid") Long uid,@Param("combinationId") Long combinationId,@Param("productId") Long productId);

    @Select("SELECT IFNULL(sum(d.cart_num),0) AS COUNT " +
            "FROM yx_store_order_cart_info a " +
            "LEFT JOIN yx_store_cart d ON d.id =a.cart_id "+
            "LEFT JOIN yx_store_product b ON b.id =a.product_id " +
            "LEFT JOIN yx_store_order c ON a.oid = c.id " +
            "WHERE c.uid=#{uid} and c.seckill_id=#{seckillId} and a.product_id=#{productId} and c.is_del =0 and c.paid=1" )
    String getOrderCountBySeckill4apay(@Param("uid") Long uid,@Param("seckillId") Long seckillId,@Param("productId") Long productId);

}
